<template>
  <div class="container" v-loading="loading">
    <div class="content content-left">
      <p class="title">
        目标客户
        <el-checkbox class="checkbox" v-model="justSeeMe" @change="getDetail">只看与我有关</el-checkbox>
      </p>
      <!-- 回访记录 -->
      <div class="customer-list">
        <el-empty :image-size="100" v-if="list.length === 0" description="暂无数据"></el-empty>
        <el-timeline class="list">
          <el-timeline-item v-for="item in list" :key="item.id">
            <div class="item" @click="handleCustomerClick(item)" :class="{ active: currentItem.id === item.id }">
              <div class="line">
                <div class="label">企业名称：</div>
                <div class="value">{{ item.com_name }}</div>
              </div>
              <div class="line">
                <div class="label">联系人：</div>
                <div class="value">{{ item.user_name }}</div>
              </div>
              <div class="line">
                <div class="label">联系电话：</div>
                <div class="value">{{ item.user_phone }}</div>
              </div>
              <div class="line">
                <div class="label">跟进人：</div>
                <div class="value">{{ item.follow_name }}</div>
              </div>
              <div class="tags">
                <i v-if="isToday(item.alarm_time)" class="el-icon-message-solid tag"></i>
                <el-tag size="small" class="tag" v-if="item.level">{{ item.level }}</el-tag>
                <el-tag size="small" class="tag">{{ item.is_success === 1 ? '已成交' : '跟进中' }}</el-tag>
              </div>
              <div class="success" v-if="item.is_success === 1">
                <img src="@/assets/img/deal.png" class="img-deal" />
              </div>
            </div>
          </el-timeline-item>
        </el-timeline>
      </div>
    </div>
    <div class="content content-right" v-if="currentItem">
      <p class="title">客户信息</p>
      <!-- 处置信息 -->
      <div class="info" v-if="detail.info">
        <!-- 状态 -->
        <div class="tags">
          <el-tag size="small" class="tag">{{ currentItem.is_success === 1 ? '已成交' : '跟进中' }}</el-tag>
          <el-tag size="small" class="tag" v-if="currentItem.level">{{ currentItem.level }}</el-tag>
        </div>
        <div class="item">
          <div class="label">企业名称：</div>
          <div class="value">
            {{ detail.info.com_name }}
            <el-tag size="small">{{ vipName }}</el-tag>
          </div>
        </div>
        <div class="item">
          <div class="label">联系人：</div>
          <div class="value">{{ detail.info.user_name }}</div>
        </div>
        <div class="item">
          <div class="label">联系电话：</div>
          <div class="value">{{ detail.info.user_phone }}</div>
        </div>
        <div class="item">
          <div class="label">是否已加微信：</div>
          <div class="value">{{ detail.info.is_add_wx === 1 ? '是' : '否' }}</div>
        </div>
        <div class="item">
          <div class="label">跟进人：</div>
          <div class="value">{{ detail.info.follow_name }}</div>
        </div>
      </div>
      <!-- 回访提醒 -->
      <div class="alarm" v-if="detail.info && detail.info.alarm_time">
        <p class="text">回访提醒：{{ detail.info.alarm_time }}</p>
        <i class="el-icon-message-solid"></i>
      </div>
      <el-empty :image-size="100" v-if="detail.list && detail.list.length === 0" description="暂无记录"></el-empty>
      <el-timeline class="contact-list" v-if="detail.list">
        <el-timeline-item v-for="(item, index) in detail.list" :key="index">
          <div class="item">
            <!-- 1 APP推送 2 微信服务通知 3 短信推送 4 AI电话推送 5 客户端查看动作 6 客户反馈 7 人工记录 -->
            <div class="left" v-if="item.type === 4">
              <!-- AI电话 -->
              <p class="text bold">
                {{ item.name }}-<span :style="{ color: getStsItem(item.sts, 'color') }">
                  {{ getStsItem(item.sts, 'name') }}
                </span>
              </p>
              <p class="text">成交意向:{{ item.ai_level }}</p>
            </div>
            <div class="left bold" v-else-if="item.type === 5">
              <!-- 客户端查看动作 -->
              <p class="text bold">{{ item.name }}</p>
            </div>
            <div class="left" v-else-if="item.type === 6">
              <!-- 客户反馈 -->
              <p class="text bold">{{ item.name }}</p>
              <p class="text">反馈内容：{{ item.desc }}</p>
            </div>
            <div class="left" v-else-if="item.type === 7">
              <!-- 人工记录 -->
              <p class="text bold">
                {{ item.name }}<span v-if="item.create_use">-{{ item.create_use }}</span>
              </p>
              <p class="text">成交意向：{{ item.level }}</p>
              <p class="text">备注：{{ item.desc }}</p>
              <p class="text">客户意见反馈：{{ item.feedback }}</p>
            </div>
            <div class="left" v-else>
              <p class="text bold">
                {{ item.name }}-
                <span :style="{ color: getStsItem(item.sts, 'color') }">
                  {{ getStsItem(item.sts, 'name') }}
                </span>
              </p>
            </div>
            <div class="right">
              <p class="text" v-if="item.push_time">{{ item.type === 7 ? '联系' : '推送' }}时间：{{ item.push_time }}</p>
              <p class="text color" v-if="item.type === 4" @click="handleAudioClick(item)">
                通话录音
                <i class="el-icon-video-play"></i>
              </p>
            </div>
          </div>
        </el-timeline-item>
      </el-timeline>
      <el-button size="mini" type="primary" @click="handleAddContact">添加人工联系记录</el-button>
    </div>
    <div class="content">
      <p class="title">处置信息</p>
      <PushDetailLeft :customerList="list" :has-deal="true" :sts-list="stsList" :id="type === 'url' ? urlPostId : id"></PushDetailLeft>
    </div>

    <!-- 添加人工回访记录 -->
    <el-dialog title="添加人工回访记录" :visible.sync="showManualContact" :modal="false">
      <ManualContact v-on="$listeners" v-if="showManualContact" :id="currentItem.id" :user-id="currentItem['user_id']" @confirm="handleContactConfirm" @close="showManualContact = false" />
    </el-dialog>
  </div>
</template>
<script>
import { apiFollowList, apiPushLeftContact, apiCustomerInfo } from '@/api/assetsDisposal'
import PushDetailLeft from './PushDetailLeft'
import ManualContact from './ManualContact'

export default {
  props: {
    type: String, // click/url
    id: String,
    vipLevel: String,
    vipName: String,
    stsList: Array,
    urlRecordId: String,
    urlPostId: String,
    urlVipLevel: String,
  },
  components: { PushDetailLeft, ManualContact },
  data() {
    return {
      justSeeMe: false,
      loading: false,
      list: [],
      detail: {},
      ManualContact: [],
      pushList: [],
      showManualContact: false,
      currentItem: {},
    }
  },
  mounted() {
    this.getDetail()
  },
  methods: {
    // 获取详情
    getDetail() {
      let info = {}
      if (this.type === 'url') {
        info = {
          id: this.urlPostId,
          just_see_me: this.justSeeMe ? 1 : 0,
          vip_level: this.urlVipLevel,
        }
      } else {
        info = {
          id: this.id,
          just_see_me: this.justSeeMe ? 1 : 0,
          vip_level: this.vipLevel,
        }
      }
      const formData = new FormData()
      for (let key in info) {
        formData.set(key, info[key])
      }
      apiFollowList(formData).then((res) => {
        if (res.code !== 200) {
          return
        }
        this.list = res.data
        if (this.type === 'url') {
          const item = this.list.find((item) => item.id === this.urlRecordId)
          if (item) {
            this.currentItem = item
          } else {
            this.currentItem = this.list[0]
          }
        } else {
          this.currentItem = this.list[0]
        }
        this.getCustomerDetail()
      })
    },
    // 获取客户详情
    getCustomerDetail() {
      const info = {
        id: this.currentItem.id,
      }
      const formData = new FormData()
      for (let key in info) {
        formData.set(key, info[key])
      }
      apiCustomerInfo(formData).then((res) => {
        if (res.code !== 200) {
          return
        }
        this.detail = res.data
      })
    },
    // 获取执行状态
    getStsItem(sts, type) {
      const stsList = [
        { sts: 0, name: '未执行', color: '#909399' },
        { sts: 1, name: '推送成功', color: '#67C23A' },
        { sts: 2, name: '未勾选此方式', color: '#909399' },
        { sts: 3, name: '推送失败', color: '#F56C6C' },
      ]
      const item = stsList.find((item) => parseInt(item.sts) === parseInt(sts))
      if (item) {
        return item[type]
      } else {
        return ''
      }
    },
    handleAudioClick(item) {
      this.$emit('audio', item)
    },

    // 添加人工联系记录
    handleAddContact() {
      this.showManualContact = true
    },
    // 添加回访记录
    handleContactConfirm() {
      this.showManualContact = false
      this.getCustomerDetail()
      this.$message.success('操作成功')
    },
    handleCustomerClick(item) {
      this.currentItem = item
      this.getCustomerDetail()
    },
    // 是否显示提醒--列表到提醒当天显示
    isToday(time) {
      if (!time) {
        return false
      }
      const today = new Date()
      const date = new Date(time)
      return date.getDate() === today.getDate() && date.getMonth() === today.getMonth() && date.getFullYear() === today.getFullYear()
    },
  },
}
</script>
<style scoped lang="scss">
.container {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  height: 100%;
  .content {
    width: 50%;
    padding: 0 24px;
    padding-bottom: 50px;
    height: 100%;
    overflow: hidden;
    overflow-y: auto;
    > .title {
      margin: 0 0 10px;
      font-weight: bold;
      font-size: 14px;
    }
  }
}

.alarm {
  width: 100%;
  background: #efefef;
  padding: 10px 20px;
  font-size: 14px;
  color: #333333;
  margin-bottom: 30px;
  position: relative;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.info {
  width: 100%;
  background: #efefef;
  padding: 10px 20px;
  font-size: 14px;
  color: #333333;
  margin-bottom: 30px;
  position: relative;
  .item {
    display: flex;
    justify-content: flex-start;
    align-items: flex-start;
    line-height: 2;

    .label {
      width: 100px;
      flex-shrink: 0;
    }
  }
  .tags {
    position: absolute;
    top: 10px;
    right: 10px;
    .tag {
      margin-left: 10px;
    }
  }
}

.customer-list {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  .list {
    font-size: 14px;
    line-height: 2;
    width: 100%;
    .item {
      padding: 10px;
      width: 100%;
      background: #efefef;
      position: relative;
      border: 1px solid transparent;
      &.active {
        border: 1px solid #d9ecff;
        background: #ecf5ff;
      }
      cursor: pointer;
      .line {
        width: 100%;
        display: flex;
        p {
          margin-top: 0;
        }
        .label {
          color: #666;
          font-weight: bold;
        }
      }
      .line-item {
        width: 50%;
        display: flex;
      }
      .tags {
        position: absolute;
        right: 2px;
        bottom: 2px;
        .tag {
          margin-left: 10px;
        }
      }
      .success {
        opacity: 0.8;
        width: 50px;
        height: 50px;
        position: absolute;
        top: 10px;
        right: 10px;
        .img-deal {
          width: 100%;
        }
      }
    }
  }
}

.contact-list {
  width: 100%;
  font-size: 12px;
  .bold {
    font-weight: bold;
  }
  .item {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    .color {
      color: #409eff;
      cursor: pointer;
    }
    p {
      margin-top: 5px;
    }
  }
}
.checkbox {
  margin-left: 30px;
}
.content-right {
  display: flex;
  flex-direction: column;
  align-items: center;
  .tags {
    top: auto;
    bottom: 10px !important;
  }
}

.el-icon-message-solid {
  font-size: 20px;
  color: #f56c6c;
}
</style>
